package com.lemon.secondhandplatform.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.Order;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderHolder> {

    private OnOrderItemClickListener onOrderItemClickListener;

    private Context context;
    private List<Order> data;

    public OrderAdapter(Context context, List<Order> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public OrderHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order, parent, false);
        return new OrderHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderHolder holder, int position) {
        Order order = data.get(position);
        holder.bind(order);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    /**
     * 刷新数据
     *
     * @param data
     */
    public void refresh(List<Order> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    /**
     * 追加数据
     *
     * @param data
     */
    public void append(List<Order> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void setOnOrderItemClickListener(OnOrderItemClickListener onOrderItemClickListener) {
        this.onOrderItemClickListener = onOrderItemClickListener;
    }

    class OrderHolder extends RecyclerView.ViewHolder {

        private Order order;

        //用户头像
        @BindView(R.id.imgSellerAvator)
        CircleImageView imgSellerAvator;
        //商品略缩图
        @BindView(R.id.imgGoodPhoto)
        ImageView imgGoodPhoto;
        //商品介绍
        @BindView(R.id.tvGoodIntroduction)
        TextView tvGoodIntroduction;
        //商品价格
        @BindView(R.id.tvGoodPrice)
        TextView tvGoodPrice;

        public OrderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(Order order) {
            this.order = order;
            Glide.with(context)
                    .load(order.getSeller().getAvator())
                    .into(imgSellerAvator);
            Glide.with(context)
                    .load(order.getThumbnailURL())
                    .into(imgGoodPhoto);
            tvGoodIntroduction.setText(order.getIntroduction());
            tvGoodPrice.setText(order.getGoodPrice().toString());
        }

        /**
         * 取消订单
         * @param v
         */
        @OnClick(R.id.tvCancelReserve)
        public void cancelReserve(View v) {
            if (onOrderItemClickListener != null) {
                onOrderItemClickListener.onCancelReserveClick(v, order);
            }
        }

        /**
         * 完成交易
         * @param v
         */
        @OnClick(R.id.tvCompleteTransaction)
        public void completeTransaction(View v) {
            if (onOrderItemClickListener != null) {
                onOrderItemClickListener.onCompleteTransactionClick(v, order);
            }
        }
    }

    public interface OnOrderItemClickListener {

        /**
         * 取消订单
         * @param v
         * @param order
         */
        void onCancelReserveClick(View v, Order order);

        /**
         * 完成交易
         * @param v
         * @param order
         */
        void onCompleteTransactionClick(View v, Order order);
    }
}
